#include <stdlib.h>
#include <sys/msg.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sched.h>
#include <dlfcn.h>
#include <sys/shm.h>
#include<map>
#include <locale>
#include <string.h>
#include <stdio.h>
class prueba{

public:
    
    int valor;
    void *p_this;
    
    prueba(int &v){
        valor=v;
        p_this=this;
    }
    virtual ~prueba(){}
    
    static void *start(void *pi){
        prueba *c=(prueba*)pi;
        c->do_it();
    }
    void do_it()
    {
        while(true){
            std::cout<<"valor: "<<valor<<std::endl;
            valor++;
            usleep(1000000);
        }
    }
    
};

int main(){
    
    int value=125;
    prueba p(value);
    pthread_t init_1, init_2;
    void *e=p.p_this;
    pthread_create(&init_1,NULL,prueba::start,e);
    sleep(5);
    pthread_create(&init_2,NULL,prueba::start,e);

}








/*#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <vector>

 inline unsigned int Quimera_server::Get_index(bool *list){
    int MID_BS=(MAX_BUFFER_SIZE/2);
    
    for(int pos=0;pos<MID_BS;pos++){
        if(*(list+MID_BS+pos)){
            return (MID_BS+pos);
        } 
        else if (*(list+MID_BS-pos)){ 
            return (MID_BS-pos);
        }
    }
}

typedef struct Payload{
        unsigned int                     size;
        void                            *buff;
}Buffer;
typedef struct c
    {
        int msg[3];
        char mss[16];

    };

typedef struct Header{ 
        char                            version;
        unsigned char                   domain;
        unsigned char                   type_message;
        unsigned int                    n_register;
        unsigned int                    id_server_source;
        unsigned int                    id_server_destination;
        char                            flag;
        unsigned short                  offset;
}Header;
 
typedef struct Package{
        Header                          header;
        unsigned short                  f_time;
        struct sockaddr_in              id_client; 
        unsigned int                    sequence_number;
        unsigned int                    acknowledge_number;
        unsigned int                    next_sequence_number;
        unsigned int                    id_server_guardian;
        struct sockaddr_in              ip_server_guardian;
}Package;

typedef struct Book_msg{
    Book_msg(Package &, Buffer &);
    Package p;
    std::vector<int>payload;  
}Book;

int main(){
    
    int newsockfd, n_bytes_r;
    struct sockaddr_in serv_addr, cli_addr;
    unsigned int cli_leng=sizeof(cli_addr);
    Package *pkg;
    Book *book;
    char buffer[1500];
    bzero((char *) &serv_addr, sizeof(serv_addr));
    bzero((char *) &cli_addr, sizeof(cli_addr));
    bzero((char *) &pkg, sizeof(pkg));
    bzero((char *) &buffer, sizeof(buffer));
    newsockfd = socket(AF_INET, SOCK_DGRAM, 0);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(3000);
    std::string stringRead; 
    bind(newsockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr));
    n_bytes_r=recvfrom(newsockfd, &buffer,sizeof(buffer), 0, (struct sockaddr *)&cli_addr, &cli_leng);
    std::cout<<"N bytes recibidos: "<<n_bytes_r<<std::endl;
    pkg=(Package*)&buffer; 
    std::cout<<"El offset es de: "<<pkg->header.offset<<std::endl;
    struct c *p;
    p=(struct c*)&buffer[sizeof(Package)];
    //for(int i=0;i<=pkg->header.offset;i+=4)
    std::cout<<p->mss<<std::endl;
    
   
    
   
    
    
    
    
}

 /* int sockfd, n;
            struct sockaddr_in serv_addr;
            unsigned int serv_leng=sizeof(serv_addr);
            bzero((char *) &serv_addr, sizeof(serv_addr));
            sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            serv_addr.sin_family = AF_INET;
            serv_addr.sin_addr.s_addr=inet_addr("10.0.0.5");
            serv_addr.sin_port = htons(3000);
            
             n = sendto(sockfd, packet,packetSize, 0, (struct sockaddr *)&serv_addr, serv_leng);
       
            std::cout<<"Bytes enviados: "<<n<<std::endl;
            std::cout<<"offset: "<<packet->p.header.offset<<std::endl;
            //close(sockfd);
*/

/*
void imprimir(Message &m){
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Tipo de mensaje:          | "<<int(m.pkg.header.type_message)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Version:                  | "<< int(m.pkg.header.version)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Numero de registro:       | "<<int(m.pkg.header.n_register) <<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Dominio:                  | "<<int(m.pkg.header.domain)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Id_s_dst:                 | " <<int(m.pkg.header.id_server_destination)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Id_s_src:                 | " <<int(m.pkg.header.id_server_source)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Flags:                    | "<<int(m.pkg.header.flag)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Offset:                   | " <<int(m.pkg.header.offset)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"Tiempo de descarte:       | " <<int(m.pkg.f_time)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"ID_Cliente:               | " <<inet_ntoa(m.pkg.id_client.sin_addr)<<"::"<<ntohs(m.pkg.id_client.sin_port)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"ack:                      | " <<int(m.pkg.acknowledge_number)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"seq:                      | " <<int(m.pkg.sequence_number)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"N_seq:                    | " <<int(m.pkg.next_sequence_number)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"IP_Servidor_Guardian:     | " <<inet_ntoa(m.pkg.ip_server_guardian.sin_addr)<<std::endl;
    std::cout <<"-----------------------------------------------------"<<std::endl;
    std::cout <<"ID_Servidor_Guardian:     | " <<int(m.pkg.id_server_guardian)<<std::endl;
    std::cout <<"-----------------------------------------------------\n\n\n";
}*/

/*int main()
{
    Message m;
    unsigned char domain='A';
    unsigned int id_ss=30;
    unsigned int id_sd=40;
    unsigned short f_t=3600;
    int packer_queue_id;
    Message_queue inbound_mail;
    struct sockaddr_in id;
    id.sin_port = htons(3000);
    id.sin_addr.s_addr=inet_addr("192.168.1.3");
    unsigned int ack=65;
    unsigned int seq=255;
    unsigned int n_seq=511;
    struct sockaddr_in guard;
    guard.sin_addr.s_addr=inet_addr("100.100.100.1");
    unsigned int id_sg=23;
    Buffer buff;
    int msg[]={65,80,81};
    char mss[]="Hola como te va";
    typedef struct c
    {
        int msg[3];
        char mss[16];

    };
    struct c mssg;
    mssg.msg[0]=msg[0];
    mssg.msg[1]=msg[1];
    mssg.msg[2]=msg[2];
    strcpy(mssg.mss,mss);
    buff.size=sizeof(mssg);
    buff.buff=(void*)&mssg;
    
    
    
    packer_queue_id=Message_queue_setup(C_PACKER_QUEUE_KEY);
    std::cout <<"La llave es: "<< packer_queue_id <<std::endl;
    inbound_mail.id_message=3;
    
    m.Fill_header(domain,id_ss,id_sd);
    m.Membership();
    imprimir(m);
    inbound_mail.package=m.pkg;
    msgsnd(packer_queue_id, (struct msgbuf *)&inbound_mail,
    sizeof(inbound_mail)-sizeof(long),0);
            
    m.Investment(f_t);
    imprimir(m);
    inbound_mail.package=m.pkg;
    msgsnd(packer_queue_id, (struct msgbuf *)&inbound_mail,
    sizeof(inbound_mail)-sizeof(long),0);
    
    m.Guard_report(id);
    imprimir(m);
    inbound_mail.package=m.pkg;
    msgsnd(packer_queue_id, (struct msgbuf *)&inbound_mail,
    sizeof(inbound_mail)-sizeof(long),0);
    
    m.Missing_report(id,ack,seq,n_seq);
    imprimir(m);
    
    inbound_mail.package=m.pkg;
    msgsnd(packer_queue_id, (struct msgbuf *)&inbound_mail,
    sizeof(inbound_mail)-sizeof(long),0);
    
    m.Adoption_papers(id,ack,seq,n_seq,guard,id_sg);
    imprimir(m);
    inbound_mail.package=m.pkg;
    msgsnd(packer_queue_id, (struct msgbuf *)&inbound_mail,
    sizeof(inbound_mail)-sizeof(long),0);
    
    m.Bookmark(id,ack,seq,n_seq,guard,id_sg,buff);
    imprimir(m);
    inbound_mail.package=m.pkg;
    inbound_mail.dat_size=m.pkg.header.offset;
    inbound_mail.data=m.packet->payload;
    msgsnd(packer_queue_id, (struct msgbuf *)&inbound_mail,
    sizeof(inbound_mail)-sizeof(long),0);
  

    m.Confirmation(m.pkg);
    imprimir(m);
    
}*/